-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.c
57 lines (44 loc) · 1.15 KB
/
Solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int graph[MAX][MAX], visited[MAX], n, queue[MAX], front = 0, rear = -1;
void BFS(int start) {
visited[start] = 1;
queue[++rear] = start;
while (front <= rear) {
int node = queue[front++];
printf("%d ", node);
for (int i = 1; i <= n; i++) {
if (graph[node][i] && !visited[i]) {
visited[i] = 1;
queue[++rear] = i;
}
}
}
}
int main() {
int edges, u, v;
printf("Enter the number of nodes: ");
scanf("%d", &n);
printf("Enter the number of edges: ");
scanf("%d", &edges);
// Initialize graph and visited arrays
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
graph[i][j] = 0;
}
visited[i] = 0;
}
printf("Enter the edges (u v):\n");
for (int i = 0; i < edges; i++) {
scanf("%d %d", &u, &v);
graph[u][v] = 1;
graph[v][u] = 1; // For undirected graph
}
int start;
printf("Enter the starting node: ");
scanf("%d", &start);
printf("BFS Traversal: ");
BFS(start);
return 0;
}